package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.thrift.RPCClient;
import com.iqiyi.kiwi.thrift.RPCException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by karkarrotxu on 2015/4/1.
 */
public final class ProductImgUtil {
    public static Logger logger = LoggerFactory.getLogger(ProductImgUtil.class);

    private ProductImgUtil() {
    }

    public static void productImgChannel(Long fatherId, JSONArray imageDatas) {
        productImg(fatherId, imageDatas, Constants.EXTEND_NEW_EPG_CHANNEL);
    }

    public static String productImgProgram(Long fatherId, JSONArray imageDatas) {
       return  productImg(fatherId, imageDatas, Constants.EXTEND_NEW_EPG_PROGRAM);
    }

    public static String getChannelImg(Long fatherId) {
        return getProductImg(fatherId, Constants.EXTEND_NEW_EPG_CHANNEL);
    }

    public static String getProgramImg(long fatherId) {
        return getProductImg(fatherId, Constants.EXTEND_NEW_EPG_PROGRAM);
    }

    private static String productImg(long fatherId, JSONArray imageDatas, String extend) {
        logger.info("[ProductImgUtil][productImg][fatherId={}, extend={}", fatherId, extend);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("entityId", fatherId);
        params.put("entityType", "99");
        params.put("businessType", "6");
        params.put("operatorId", "6");
        params.put("objectTitle", "epg");
        params.put("imageDatas", imageDatas);
        params.put("extend", extend);
       return  doProduct(fatherId,params);
    }

    private static String doProduct(long fatherId,  Map<String, Object> params  ){
        long startTime = System.currentTimeMillis();
        String sendParams = JSON.toJSONString(params);
        String resultValue = null;
        logger.info("[doProduct][fatherId={}][params={}]",fatherId,sendParams);
        try {
            com.iqiyi.kiwi.thrift.RPCClient imageRpcClient = (RPCClient) ApplicationContextUtil.getBean("imageRpcClient");
             resultValue = (String) imageRpcClient.executeMethod("imageManufactureBatch", "binary", new Class[]{String.class}, new Object[]{sendParams});
            logger.info("response :"+ resultValue);
        } catch (RPCException e) {
            logger.error("[doProduct][fatherId="+fatherId+"][params="+sendParams+"]", e);
        }
        long endTime = System.currentTimeMillis();
        logger.info("[doProduct][fatherId={}][time={}]",fatherId,(endTime-startTime));
        return resultValue;
    }

    public static void productMultiPanoImg( String extend,long fatherId){

    }


    private static String getProductImg(Long fatherId, String extend) {
        logger.info("[ProductImgUtil][getProductImg][fatherId={}, extend={}", fatherId, extend);
        String resultValue = "";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("entityId", fatherId);
        params.put("entityType", "99");
        params.put("businessType", "6");
        params.put("operatorId", "6");
        params.put("objectTitle", "epg");
        params.put("useTypes", "699");
        params.put("extend", extend);
        long startTime = System.currentTimeMillis();
        try {
            com.iqiyi.kiwi.thrift.RPCClient imageRpcClient = (RPCClient) ApplicationContextUtil.getBean("imageRpcClient");
            resultValue = (String) imageRpcClient.executeMethod("echoEditImages", "binary", new Class[]{String.class}, new Object[]{JSON.toJSONString(params)});
            logger.info("[getProductImg]response :"+ resultValue);
            JSONObject jsonObject = JSON.parseObject(resultValue);
            if (jsonObject.getString("code").equals("A00000")) {
                resultValue = jsonObject.getString("data");
            }
        } catch (RPCException e) {
            logger.error("[ProductImgUtil][getProductImg][fatherId:"+fatherId, e);
        }
        long endTime = System.currentTimeMillis();
        logger.info("[ProductImgUtil][getProductImg][fatherId={}][time={}]",fatherId,(endTime-startTime));
        return resultValue;
    }
}
